<?php
namespace app\front\controller;

use think\App;
use think\facade\Db;
use app\BaseController;
use app\tools\logic\CommonLogic;
use app\tools\logic\ApiLogic;
use app\tools\logic\LoginLogic;
//基础控制器
class Base extends BaseController{
	protected $lang = 'zh';				//默认语言，中文
	protected $exchange_rate = 1;			//汇率，中文为1，英文为后台设置的
	protected $currency_symbol = '￥';		//设置默认货币符号
	protected $member_id = 0;					 //当前用户的ID
	protected $total_amount = 0;				 //当前账户的余额
	protected $member_info = [];				 //用户信息
	protected $token = "";
	protected $ltc_rate = 1;					 //LTC汇率
	protected $usdt_rate = 1;					 //USDT汇率
	protected $has_login = 1;					 //1表示已经登录了

	protected $channel_id = 0;			//通道
	protected $channel_type = 0;		//通道类型【针对一个通道里面有多个api地址的情况，默认为0】
	protected $country_id = 0; 			//国家
	protected $server_id = 0;			//服务
	protected $country_code;		//国家代码
	protected $server_code;			//服务代码
	protected $item_exchange_rate=1;
	protected $item_base_rate=1;
	protected $is_ok=1;

	protected $baseInfo;

	public function __construct(App $app){
		parent::__construct($app);

		//语言设置，货币设置
		$this->lang = $this->request->header('lang');
		$this->country_id = input('country_id', 0);	//国家
		$this->server_id = input('server_id', 0);	//服务

		if($this->server_id){
			//如果传入了服务，通过服务来获取通道的信息
			$this->getChannelInfoByServerId($this->country_id, $this->server_id);
		}elseif($this->country_id){
			//如果没有服务有国家，通过国家来获取通道的信息
			$this->getChannelInfoByCountryId($this->country_id);
		}
		//如果是英文站，需要转化成英语和美元
		if($this->lang=='en'){
			$this->getChannelEnglishCurrency();
		}

		//账号登陆验证
		$this->token = $this->request->header('token');
		if($this->token){
			$this->getMemberInfo();
		}else{
			$this->has_login = 401;
		}
		//获取汇率
		$this->getLtcUsdtInfo();
		
	}
	//获取汇率
	function getLtcUsdtInfo(){
		//获取LTC汇率
		$ltcInfo = Db::name('config')->field('value')->where('name','ltc_rate')->find();
		$this->ltc_rate = $ltcInfo['value'];
		//获取USDT汇率
		$usdtInfo = Db::name('config')->field('value')->where('name','usdt_rate')->find();
		$this->usdt_rate = $usdtInfo['value'];
		if($this->lang=='en'){
			$this->ltc_rate = round($this->ltc_rate / $this->exchange_rate, 6);
			$this->usdt_rate = round($this->usdt_rate / $this->exchange_rate, 6);
		}
	}
	//获取用户信息
	function getMemberInfo(){
		//查询账户信息
		$fields = "m.id,
				 m.user_name,
				 round(ma.total_amount,2) as total_amount,
				 round(ma.recharge_total_amount,2) as recharge_total_amount,
				 m.email,
				 m.qq,
				 m.status,
				 m.grade_level,
				 m.invitation_code,
				 m.api_key,
				 m.create_time";
		if($this->lang=='zh'){
			$fields .= ",mg.grade_name";
		}else{
			$fields .= ",mg.grade_name_en as grade_name";
		}
		$this->member_info = Db::name('member')
					->alias('m')
					->field($fields)
					->join('member_account ma','ma.member_id=m.id')
					->join('member_grade mg','mg.level=m.grade_level')
					->where('m.token', $this->token)
					->find();
		if($this->member_info){
			$this->member_id = $this->member_info['id'];
			if($this->lang=='en'){
				$this->total_amount = round($this->member_info['total_amount'] / $this->exchange_rate,2) ;
				$this->member_info['total_amount'] = round($this->member_info['total_amount'] / $this->exchange_rate,2) ;
				$this->member_info['recharge_total_amount'] = round($this->member_info['recharge_total_amount'] / $this->exchange_rate,2) ;
				$this->member_info['currency_symbol'] = '$';
			}else{
				$this->total_amount = round($this->member_info['total_amount'],2);
				$this->member_info['currency_symbol'] = $this->currency_symbol;
			}
		}else{
			$this->has_login = 402;
		}
	}
	//获取当前货币转成人名币再转成美元
	function getChannelEnglishCurrency(){
		$info = Db::name('channel_currency')
					->field('id,exchange_rate')
					->where('currency_code','USD')
					->where('status', 1)
					->order('id asc')
					->find();
		if(!$info){
			$this->is_ok = 0;
			return false;
		}else{
			//因为所有的货币设置都是1兑多少人名币，所以人名币转化过来的时候需要除以这个数。
			$this->item_exchange_rate = $this->item_exchange_rate / $info['exchange_rate'];
			$this->exchange_rate = $info['exchange_rate'];
			$this->currency_symbol = '$';
		}

		return true;
	}
	//根据国家获取信息
	function getChannelInfoByCountryId($country_id=0){
		if(!$country_id){
			$this->is_ok = 0;
			return false;
		}
		$info = Db::name('channel_country')
				->field('cc.id as country_id,
						 cc.channel_id,
						 cc.channel_type,
						 cc.country_name,
						 cc.country_zh,
						 cc.country_en,
						 cc.country_code,
						 cc.base_rate,
						 c.channel_name,
						 c.base_rate as c_base_rate, 
						 cct.exchange_rate
					')
				->alias('cc')
				->join('channel c','c.id=cc.channel_id')
				->join('channel_currency cct','cct.id=c.currency_id')
				->where('cc.id', $country_id)
				->where('cc.status', 1)
				->find();
		// print_r($info);
		if($info){
			$this->channel_id = $info['channel_id'];
			$this->country_id = $info['country_id'];
			$this->country_code = $info['country_code'];
			//相对人民币的汇率
			$this->item_exchange_rate = $info['exchange_rate'];
			$this->channel_type = $info['channel_type'];
			//国家和通道的价格基数，国家优先
			$this->item_base_rate = 1;
			$this->baseInfo = $info;
		}else{
			$this->is_ok = 0;
		}
		return true;
	}
	//根据服务获取信息
	function getChannelInfoByServerId($country_id=0,$server_id=0){
		if(!$server_id || !$country_id){
			$this->is_ok = 0;
			return false;
		}

		$info = Db::name('channel_country_server')
				->field('cc.id as country_id,
						 cc.channel_id,
						 cc.channel_type,
						 cc.country_name,
						 cc.country_zh,
						 cc.country_en,
						 cc.country_code,
						 ccs.base_rate,
						 c.channel_name,
						 cct.exchange_rate,
						 cs.id as server_id,
						 cs.server_name,
						 cs.server_zh,
						 cs.server_en,
						 cs.server_code
					')
				->alias('ccs')
				->join('channel_country cc','cc.id=ccs.channel_country_id')
				->join('channel_server cs','cs.id=ccs.channel_server_id')
				->join('channel c','c.id=cc.channel_id')
				->join('channel_currency cct','cct.id=c.currency_id')
				->where('ccs.channel_country_id', $country_id)
				->where('ccs.channel_server_id', $server_id)
				->find();

		if($info){
			$this->channel_id = $info['channel_id'];
			$this->channel_type = $info['channel_type'];
			$this->country_id = $info['country_id'];
			$this->country_code = $info['country_code'];
			//相对人民币的汇率
			$this->item_exchange_rate = $info['exchange_rate'];	
			//国家和通道的价格基数，国家优先
			$this->item_base_rate = $info['base_rate'];
			$this->server_id = $info['server_id'];
			$this->server_code = $info['server_code'];
			$this->baseInfo = $info;
		}else{
			$this->is_ok = 0;
		}
		return true;
	}
}
